//
//  CAEAGLLayerViewController.m
//  CoreAnimation
//
//  Created by 张国文 on 16/10/24.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "CAEAGLLayerViewController.h"
#import "OpenGLUseCAEAGLLayerViewController.h"
#import "OpenGLUseGLKViewViewController.h"
#import "FilterViewController.h"
#import "MixedFiltersViewController.h"

@interface CAEAGLLayerViewController ()

@end

@implementation CAEAGLLayerViewController

/**
 *  遇见的问题：OpenGL ES渲染过后再使用滤镜会造成程序崩溃，欢迎大神指出问题。
 */

/**
*  OpenGL ES是直接和硬件进行交互的，因此渲染速度非常之快，我们通常运用OpenGL ES和CIContext联合使用，CIContext和OpenGL ES联合使用的时候会运用GPU渲染图形到EAGLContext上面，但是cpu仍然会进行少量的运算，因此在使用的时候我们会看到cpu仍然会消耗，但是消耗率非常小并且不会造成程序卡顿。
*/

/**
*  CoreImage处理图像的流程：

1:创建一个新的CIImage；

2:创建一个行的CIFIlter,并通过键-值设置各种输入值，这些值有些是有默认值的，有些没有默认值，需要编程者的设置；

3:冲CIFilter中生成输出图像，如果存在滤镜链则将输出图像作为输入参数传入到下一个滤镜，跳回步骤2继续进行，如果到达滤镜末，则调用CIContext渲染CIImage对象。这个context可以是基于CPU或GPU的，基于CPU的产出CGImageRef对象，基于GPU的调用OpenGL ES在屏幕上画出结果，默认是基于CPU的。
*/

/**
*  我们的前两个按钮中使用的是OpenGL ES技术，后两个滤镜使用的CoreImage技术。你会发现OpenGL ES的优势，不会造成程序卡顿
*/

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeCustom];
    firstButton.frame = CGRectMake(0, 20, 300, 50);
    [firstButton setTitle:@"openGL使用GLKView渲染图片" forState:UIControlStateNormal];
    [firstButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    firstButton.tag = 100;
    [firstButton addTarget:self action:@selector(clickJump:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:firstButton];
    
    UIButton *sectionButton = [UIButton buttonWithType:UIButtonTypeCustom];
    sectionButton.frame = CGRectMake(0, 70, 300, 50);
    [sectionButton setTitle:@"openGL不使用GLKView自己绘制图片" forState:UIControlStateNormal];
    [sectionButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    sectionButton.tag = 101;
    [sectionButton addTarget:self action:@selector(clickJump:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sectionButton];
    
    
    UIButton *thirdButton = [UIButton buttonWithType:UIButtonTypeCustom];
    thirdButton.frame = CGRectMake(0, 120, 300, 50);
    [thirdButton setTitle:@"Filter(滤镜)" forState:UIControlStateNormal];
    [thirdButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    thirdButton.tag = 102;
    [thirdButton addTarget:self action:@selector(clickJump:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:thirdButton];
    
    UIButton *fourthButton = [UIButton buttonWithType:UIButtonTypeCustom];
    fourthButton.frame = CGRectMake(0, 170, 300, 50);
    [fourthButton setTitle:@"MixedFilter(混合滤镜)" forState:UIControlStateNormal];
    [fourthButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    fourthButton.tag = 103;
    [fourthButton addTarget:self action:@selector(clickJump:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:fourthButton];

}

- (void)clickJump:(UIButton *)button
{
    switch (button.tag) {
        case 100:
        {
            OpenGLUseGLKViewViewController *openGLUseGLKViewVC = [[OpenGLUseGLKViewViewController alloc] init];
            [self.navigationController pushViewController:openGLUseGLKViewVC animated:YES];
            break;
        }
        case 101:
        {
            OpenGLUseCAEAGLLayerViewController *openGLUseCAEAGLLayerVC = [[OpenGLUseCAEAGLLayerViewController alloc] init];
            [self.navigationController pushViewController:openGLUseCAEAGLLayerVC animated:YES];
            break;
        }
        case 102:
        {
            FilterViewController *filterVC = [[FilterViewController alloc] init];
            [self.navigationController pushViewController:filterVC animated:YES];
            break;
        }
        case 103:
        {
            MixedFiltersViewController *mixedFiltersVC = [[MixedFiltersViewController alloc] init];
            [self.navigationController pushViewController:mixedFiltersVC animated:YES];
            break;
        }
        default:
            break;
    }
}



@end
